﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using candi;

/// <summary>
///Common 的摘要说明
/// </summary>
public class Common
{
    public Common()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //


    }

    public string getHTML(string url)
    {
        string restr;
        try
        {
            WebRequest req = WebRequest.Create(url);
            WebResponse res = req.GetResponse();
            res.GetResponseStream();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            restr = reader.ReadToEnd();

            reader.Close();
            reader.Dispose();
            res.Close();
        }
        catch
        {
            restr = string.Empty;
        }
        return restr;
    }

    public List<FeedBackModel> PagingBuyerRecord(string sales)
    {
        List<FeedBackModel> flist = new List<FeedBackModel>();
        //  string url = "http://localhost:4224/feedbackfetch/HTMLPage.htm";
        string url = string.Format("http://feedback.ebay.com/ws/eBayISAPI.dll?ViewFeedback2&userid={0}&ftab=FeedbackAsSeller", sales);
        string htmlContent = getHTML(url);
        string pageContent = GetData("(?<=<span>of).*?(?=</span>)", htmlContent);
        int pcount;
        if (int.TryParse(pageContent, out pcount))
        {
            for (int i = 1; i <= pcount; i++)
            {
                flist.AddRange(BuyerRecord(sales, i));
            }
            return flist;
        }
        return null;

    }

    public List<FeedBackModel> BuyerRecord(string sales, int pageindex)
    {

        List<FeedBackModel> flist = new List<FeedBackModel>();

        // string url = "http://localhost:4224/feedbackfetch/HTMLPage.htm";
        string url = string.Format("http://feedback.ebay.com/ws/eBayISAPI.dll?ViewFeedback2&ftab=FeedbackAsSeller&userid={0}&iid=-1&de=off&items=25&interval=0&page={1}", sales, pageindex);

        Common c = new Common();

        //"(?<=<div\\s*id=\"enText\"\\s*style=\"display:block\">).*?(?=</div>)"
        string htmlContent = c.getHTML(url);
        string feedContent = c.GetData("(?<=<table\\s*border=\"0\"\\s*cellpadding=\"0\"\\s*cellspacing=\"0\"\\s*width=\"100%\"\\s*class=\"FbOuterYukon\").+(?=</table>)", htmlContent);
        //  Response.Write(c.GetData("(?<=<span\\s*class=\"mbg-nw\">).+(?=</table>)", htmlContent)[0].ToString());

        List<string> clist = c.GetListData("(?<=<tr class=\"bot\"><td>&nbsp;</td><td>).*?(?=</td><td id=\"viewItemId\">)", feedContent);
        List<string> dlist = c.GetListData("(?<=<td nowrap=\"nowrap\">).*?(?=</td>)", feedContent);

        List<string> namelist = c.GetListData("(?<=<span\\s*class=\"mbg-nw\">).*?(?=</span>)", feedContent);


        FeedBackModel fbm;
        for (int i = 0; i < clist.Count; i++)
        {


            // string[] sa = clist[i].Split("</td><td>");

            // plist.Add();
            if (clist[i].IndexOf("(#") > 0)
            {
                fbm = new FeedBackModel();
                fbm.Price = clist[i].Substring(clist[i].IndexOf("</td><td>") + 9);
                fbm.ItemName = clist[i].Substring(0, clist[i].IndexOf("(#"));
                fbm.ItemNo = clist[i].Substring(clist[i].IndexOf("(#") + 2, 12);
                if (namelist.Count > i)
                    fbm.BuyerID = namelist[i];
                if (dlist.Count > i)
                    fbm.SaleDate = dlist[i];
                //itmlist.Add();
                //itmNamlist.Add();
                flist.Add(fbm);
            }

        }
        string pageContent = c.GetData("(?<=<span>of).*?(?=</span>)", htmlContent);

        return flist;
    }

    public List<StoreList> PagingListrRecord(string sales)
    {
        List<StoreList> flist = new List<StoreList>();
        string url = string.Format("http://www.ebay.com/sch/{0}/m.html", sales);
        //string url = "http://localhost:4224/feedbackfetch/fashion.evereBay.htm";
        string htmlContent = getHTML(url);
        //<li class="toppg-t">Page 3 of 14</li>
        string pageContent = GetData("(?<=<li class=\"toppg-t\">).*?(?=</li>)", htmlContent);
        int pcount;
        pageContent = pageContent.Substring(pageContent.IndexOf("of"));
        pageContent = pageContent.Replace("of ", "");
        if (int.TryParse(pageContent, out pcount))
        {
            for (int i = 1; i <= pcount; i++)
            {
                flist.AddRange(ListRecord(sales, i));
            }
            return flist;
        }
        return null;

    }

    public List<StoreList> ListRecord(string sales, int pageindex)
    {

        List<StoreList> flist = new List<StoreList>();

        // http://www.ebay.com/sch/fashion.ever/m.html?_trkparms=65%253A1%257C66%253A2%257C39%253A1&rt=nc&_trksid=p3911.c0.m14.l1513&_pgn=4
        string url = string.Format("http://www.ebay.com/sch/{0}/m.html?_pgn={1}", sales, pageindex);
        // string url = "http://localhost:4224/feedbackfetch/fashion.evereBay.htm";
        Common c = new Common();

        //"(?<=<div\\s*id=\"enText\"\\s*style=\"display:block\">).*?(?=</div>)"
        string htmlContent = c.getHTML(url);
        string itemlistContent = c.GetData("(?<=<table\\s*cellspacing=\"0\"\\s*class=\"li\").+(?=</table>)", htmlContent);
        //  Response.Write(c.GetData("(?<=<span\\s*class=\"mbg-nw\">).+(?=</table>)", htmlContent)[0].ToString());

        //<img class="img" src="http://thumbs4.ebaystatic.com/m/mpaqjRs4Q7RmQijti7MyK6g/140.jpg" alt="Item image" i="i">
        List<string> ilist = c.GetListData("(?<=<img\\s*class=\"img\"\\s*src=\").*?(?=\" alt=\"Item image\" i=\"i\">)", itemlistContent);
        List<string> clist = c.GetListData("(?<=class=\"vip\"\\s*title=\").*?(?=\">)", itemlistContent);
        List<string> plist = c.GetListData("(?<=<td\\s*class=\"prc g-b\">).*?(?=</td>)|(?<=<div\\s*class=\"g-b\">).*?(?=</div>)", itemlistContent);//[|prc g-b]
        //List<string> llist = c.GetListData("(?<=<div\\s*class=\"ttl\"><a href=\").*?(?=\")", itemlistContent);
        List<string> tlist = c.GetListData("(?<=<td\\s*class=\"bids bin1\">).*?(?=</td>)", itemlistContent);

        //List<string> dlist = c.GetListData("(?<=<td nowrap=\"nowrap\">).*?(?=</td>)", itemlistContent);
        //  List<string> namelist = c.GetListData("(?<=<span\\s*class=\"mbg-nw\">).*?(?=</span>)", itemlistContent);


        StoreList fbm;
        for (int i = 0; i < clist.Count; i++)
        {
            // string[] sa = clist[i].Split("</td><td>");
            // plist.Add();
            if (clist[i].Length > 0)
            {
                fbm = new StoreList();
                fbm.Itemimage = ilist[i];
                fbm.Title = clist[i];
                fbm.price = plist[i];
                if (tlist[i] == "Buy It Now")
                {
                    fbm.Itemtype = "1";
                }
                else
                {
                    fbm.Itemtype = "0";
                }

                flist.Add(fbm);
            }

        }
        string pageContent = c.GetData("(?<=<span>of).*?(?=</span>)", htmlContent);

        return flist;
    }




    /// <summary>
    /// 返回分析数据
    /// </summary>
    /// <param name="regexString">正则</param>
    /// <param name="html">HTML</param>
    /// <returns>数据</returns>
    public List<string> GetListData(string regexString, string html)
    {
        List<string> result = new List<string>();
        Regex regex = new Regex(regexString, RegexOptions.IgnoreCase);
        MatchCollection mc = regex.Matches(html);
        for (int count = 0; count < mc.Count; count++)
        {
            Match m = mc[count];
            for (int index = 0; m.Groups[index].Value != ""; index++)
            {
                result.Add(m.Groups[index].Value);
            }
        }
        return result;
    }

    public string GetData(string regexString, string html)
    {
        Regex regex = new Regex(regexString, RegexOptions.IgnoreCase);
        Match mc = regex.Match(html);
        return mc.Value;

    }



}